package com.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.conf.StringRedisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.auto.*;
import com.model.auto.vo.GetSchoolV2Vo;
import com.service.*;
import com.util.ProbabilityUtils;
import com.util.R;
import com.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.repository.query.Param;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 新高考学校
 *
 * @author zhaoyifan
 */
@RestController
@RequestMapping("/schoolV2")
public class SchoolV2Controller extends BaseController{
    @Autowired
    private ISchoolV2Service schoolService;
    @Autowired
    private IMajorV2Service majorService;
    @Autowired
    private IMajorCategoryInfoService majorCategoryInfoService;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private ISchoolLabelService schoolLabelService;
    @Autowired
    private StringRedisUtils stringRedisUtils;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private ISchoolInfoV2Service schoolInfoV2Service;
    @Autowired
    private IScoreItemService scoreItemService;

    private Logger logger = LoggerFactory.getLogger(SchoolV2Controller.class);
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/deleteRedis")
    public R deleteRedis() {
        String[] regulation = {"本科提前批单设", "本科普通批单设", "高职专科批提前", "本科普通批单设", "本科普通批平行", "高职专科批普通"};
        final List<String> regulationOrder = Arrays.asList(regulation);
        for (String batch : regulationOrder) {
            deleteRedis(batch, 1);
            deleteRedis(batch, 2);
        }
        return R.ok();
    }

    private void deleteRedis(String batch, Integer type) {
        Set<String> keys = redisTemplate.keys(batch + "_" + type + "*");
        if (Objects.nonNull(keys)) {
            for (String key : keys) {
                redisTemplate.delete(key);
            }
        }
    }

    @PostMapping("/getStuSchoolInformation")
    public R getStuSchoolInformation(@RequestBody GetSchoolV2Vo getSchoolVo) {
//        List<String> majorCodeList = new ArrayList<>();
//
//        List<SchoolV2> result = new ArrayList<>();
//
//        String redisKey = getSchoolVo.getBatch()+"_"+getSchoolVo.getMajorType()+"_"+getSchoolVo.getSortType();
////        if(StringUtils.hasText(stringRedisUtils.getStringVal(redisKey))){
////            result = JSONArray.parseArray(stringRedisUtils.getStringVal(redisKey), SchoolV2.class);
////        }else{
//        LambdaQueryWrapper<SchoolV2> query = new LambdaQueryWrapper<SchoolV2>()
//                .eq(SchoolV2::getBatch, getSchoolVo.getBatch())
//                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType());
//        if (getSchoolVo.getSortType() == 1) {
//            query.orderByAsc(SchoolV2::getProposedwc);
//        } else {
//            query.orderByAsc(SchoolV2::getGroupProposedwc);
//        }
//        //如果再选科目条件不为空
//        if (!ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
//            StringBuffer subjectSql = new StringBuffer();
//            subjectSql.append(" find_in_set ('不限', subject ) or ( ");
//            for (int i = 0; i < getSchoolVo.getSubject().size(); i++) {
//                subjectSql.append(" find_in_set ('" + getSchoolVo.getSubject().get(i) + "', subject )  ");
//                if (i < getSchoolVo.getSubject().size() - 1) {
//                    subjectSql.append(" and ");
//                }
//            }
//            subjectSql.append(") ");
//            query.apply(subjectSql.toString());
//        }
//        result = schoolService.list(query);
//        stringRedisUtils.setStringVal(redisKey, JSONObject.toJSONString(result));
////        }
//
//        List<MajorV2> majorsList = null;
//        if (!StringUtil.isEmpty(getSchoolVo.getMajor1()) || !StringUtil.isEmpty(getSchoolVo.getMajor2()) || !StringUtil.isEmpty(getSchoolVo.getMajor3())) {
//            majorsList = majorService.list(new LambdaQueryWrapper<MajorV2>()
//                    .and(wrapper ->
//                            wrapper.like(!StringUtil.isEmpty(getSchoolVo.getMajor1()), MajorV2::getMajorName, getSchoolVo.getMajor1())
//                                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor2()), MajorV2::getMajorName, getSchoolVo.getMajor2())
//                                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor3()), MajorV2::getMajorName, getSchoolVo.getMajor3())
//                    )
//                    .eq(MajorV2::getType, getSchoolVo.getMajorType())
//                    .select(MajorV2::getCode, MajorV2::getId)
//                    .eq(MajorV2::getBatch, getSchoolVo.getBatch())
//            );
//            if (ObjectUtils.isEmpty(majorsList)) {
//                return R.ok(result);
//            } else {
//                majorsList.forEach(i -> majorCodeList.add(i.getCode()));
//            }
//        }
//
//        List<String> labelCode = new ArrayList<>();
//        //院校层级||博硕资格不为空
//        if (StringUtils.hasText(getSchoolVo.getHierarchys()) || StringUtils.hasText(getSchoolVo.getQualifications())) {
//            List<SchoolLabel> labelList = schoolLabelService.getByParam(getSchoolVo.getHierarchys(), getSchoolVo.getQualifications());
//            labelList.forEach(i -> labelCode.add(i.getCode()));
//        }
//        String city = getSchoolVo.getCity().toString();
//        List<SchoolLabel> schoolLabels;
//        List<Object> citys = Arrays.asList("陕西省", "安徽省", "北京市", "重庆市", "福建省", "广东省", "广西", "甘肃省", "贵州省", "河北省",
//                "河南省", "海南省", "湖北省", "湖南省", "黑龙江省", "吉林省", "江苏省", "江西省", "辽宁省", "内蒙古", "宁夏", "青海省", "上海市",
//                "四川省", "山东省", "山西省", "天津市", "西藏", "新疆", "云南省", "浙江省");
//        if (!StringUtil.isEmpty(getSchoolVo.getCity()) && city.equals("[其他]")) {
//            schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
//                    .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getCode, labelCode)
//                    .notIn(SchoolLabel::getCity, citys)
//                    .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
//                    .select(SchoolLabel::getCode)
//            );
//        } else {
//            schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
//                    .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getCode, labelCode)
//                    .in(!StringUtil.isEmpty(getSchoolVo.getCity()), SchoolLabel::getCity, getSchoolVo.getCity())
//                    .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
//                    .select(SchoolLabel::getCode)
//            );
//        }
//        List<String> codeList = schoolLabels.stream().map(SchoolLabel::getCode).collect(Collectors.toList());
//        if (codeList.size() <= 0) {
//            PageInfo<SchoolV2> pageInfos = PageHelper.startPage(getSchoolVo.getPageNum(), getSchoolVo.getPageSize()).doSelectPageInfo(() -> {
//            });
//            return R.ok(pageInfos);
//        }
//        String schoolType = getSchoolVo.getType().toString();
//        List<Object> schoolTypes = Arrays.asList("综合", "工科", "财经", "农业", "林业", "医药", "师范", "体育", "政法", "民族", "语言", "艺术");
//
//        LambdaQueryWrapper<SchoolV2> pageQuery = new LambdaQueryWrapper<SchoolV2>()
//                .in(!StringUtil.isEmpty(majorCodeList), SchoolV2::getCode, majorCodeList)
//                .in(!StringUtil.isEmpty(codeList), SchoolV2::getCode, codeList)
//                .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), SchoolV2::getBatch, getSchoolVo.getBatch())
//                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
//                .in(!ObjectUtils.isEmpty(getSchoolVo.getRecruitType()), SchoolV2::getRecruitType, getSchoolVo.getRecruitType());
//
//        if (!StringUtil.isEmpty(getSchoolVo.getType()) && schoolType.equals("[其他]")) {
//            pageQuery.notIn(SchoolV2::getSchoolType, schoolTypes);
//        } else {
//            pageQuery.in(!StringUtil.isEmpty(getSchoolVo.getType()), SchoolV2::getSchoolType, getSchoolVo.getType());
//        }
//        //如果再选科目条件不为空
//        if (!ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
//            StringBuffer subjectSql = new StringBuffer();
//            subjectSql.append(" find_in_set ('不限', subject ) or ( ");
//            for (int i = 0; i < getSchoolVo.getSubject().size(); i++) {
//                subjectSql.append(" find_in_set ('" + getSchoolVo.getSubject().get(i) + "', subject )  ");
//                if (i < getSchoolVo.getSubject().size() - 1) {
//                    subjectSql.append(" and ");
//                }
//            }
//            subjectSql.append(") ");
//            pageQuery.apply(subjectSql.toString());
//        }
//        List<SchoolV2> pageInfo = schoolService.list(pageQuery);
//
//        result.forEach(i -> {
//            pageInfo.forEach(p -> {
//                if (p.getId().equals(i.getId())) {
//                    i.setColor(1);
//                }
//            });
//        });
//        return R.ok(result);

        return getSchoolInformation(getSchoolVo);
    }

    @PostMapping("getSchoolInformations")
    public R getSchoolInformations(@RequestBody GetSchoolV2Vo getSchoolVo) {
//        if(ObjectUtils.isEmpty(getSchoolVo.getSubject())){
//            return R.error("再选科目参数不能为空");
//        }
//
//        String redisKey = getSchoolVo.getBatch()+"_"+getSchoolVo.getMajorType()+"_"+getSchoolVo.getSortType();
//        for(String subject:getSchoolVo.getSubject()){
//            redisKey += "_" + subject;
//        }
//        if(StringUtils.hasText(stringRedisUtils.getStringVal(redisKey))){
//            List<SchoolV2> schools = JSONArray.parseArray(stringRedisUtils.getStringVal(redisKey), SchoolV2.class);
//            return R.ok(schools);
//        }
//        LambdaQueryWrapper<SchoolV2> query = new LambdaQueryWrapper<SchoolV2>()
//                .eq(SchoolV2::getBatch, getSchoolVo.getBatch())
//                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType());
//        //如果再选科目条件不为空
//        if (!ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
//            StringBuffer subjectSql = new StringBuffer();
//            subjectSql.append(" (find_in_set ('不限', subject ) or ( ");
//            for (int i = 0; i < getSchoolVo.getSubject().size(); i++) {
//                subjectSql.append(" find_in_set ('" + getSchoolVo.getSubject().get(i) + "', subject )  ");
//                if (i < getSchoolVo.getSubject().size() - 1) {
//                    subjectSql.append(" and ");
//                }
//            }
//            subjectSql.append(")) ");
//            query.apply(subjectSql.toString());
//        }
//
//        if (getSchoolVo.getSortType() == 1) {
//            query.orderByAsc(SchoolV2::getProposedwc);
//        } else {
//            query.orderByAsc(SchoolV2::getGroupProposedwc);
//        }
//
//        List<SchoolV2> schools = schoolService.list(query);
//        stringRedisUtils.setStringVal(redisKey, JSONObject.toJSONString(schools));
//        return R.ok(schools);

        return getSchoolInformation(getSchoolVo);
    }

    @PostMapping("/getSchoolInformation")
    public R getSchoolInformation(@RequestBody GetSchoolV2Vo getSchoolVo) {
        List<Long> majorSchoolIdList = new ArrayList<>();

        List<SchoolV2> result = new ArrayList<>();

        if (ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
            return R.error("再选科目参数不能为空");
        }

        String redisKey = getSchoolVo.getBatch() + "_" + getSchoolVo.getMajorType() + "_" + getSchoolVo.getSortType();
        for (String subject : getSchoolVo.getSubject()) {
            redisKey += "_" + subject;
        }
        if (!ObjectUtils.isEmpty(getSchoolVo.getRecruitType())) {
            for (String recruitType : getSchoolVo.getRecruitType()) {
                redisKey += "_" + recruitType;
            }
        }

        if (StringUtils.hasText(stringRedisUtils.getStringVal(redisKey))) {
            result = JSONArray.parseArray(stringRedisUtils.getStringVal(redisKey), SchoolV2.class);
        } else {
            LambdaQueryWrapper<SchoolV2> query = new LambdaQueryWrapper<SchoolV2>()
                    .eq(SchoolV2::getBatch, getSchoolVo.getBatch())
                    .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
                    .in(!ObjectUtils.isEmpty(getSchoolVo.getRecruitType()), SchoolV2::getRecruitType, getSchoolVo.getRecruitType());

            List<String> subject = new ArrayList<>();
            subject.add("不限");
            for (String subjectParam : getSchoolVo.getSubject()) {
                subject.add(subjectParam);
            }
            if(getSchoolVo.getSubject().size()>1){
                subject.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
                subject.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
            }
            query.in(SchoolV2::getSubject, subject);

            if (getSchoolVo.getSortType() == 1) {
                query.orderByAsc(SchoolV2::getProposedwc);
            } else {
                query.orderByAsc(SchoolV2::getGroupProposedwc);
            }
            result = schoolService.list(query);
            stringRedisUtils.setStringVal(redisKey, JSONObject.toJSONString(result));
        }

        Integer precedence = getSchoolVo.getPrecedence();
        if (Objects.isNull(precedence)) {
            Student student = studentService.getById(getSchoolVo.getStuId());
            if (Objects.nonNull(student) && !StringUtil.isEmpty(student.getPrecedence()) && StringUtil.isNumeric(student.getPrecedence())) {
                precedence = Integer.valueOf(student.getPrecedence());
            }
        }

        //判断是否有查询条件
        if (StringUtil.isEmpty(getSchoolVo.getMajor1()) && StringUtil.isEmpty(getSchoolVo.getMajor2()) && StringUtil.isEmpty(getSchoolVo.getMajor3())
                && StringUtil.isEmpty(getSchoolVo.getQualifications()) && StringUtil.isEmpty(getSchoolVo.getHierarchys()) && ObjectUtils.isEmpty(getSchoolVo.getNature())
                && ObjectUtils.isEmpty(getSchoolVo.getType()) && ObjectUtils.isEmpty(getSchoolVo.getCity())) {
            for (SchoolV2 schoolV2 : result) {
                if (StringUtils.hasText(schoolV2.getSubject())) {
                    schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
                }
                getSchoolProbability(schoolV2, precedence);
            }
            R r = R.ok(result);
            r.put("searchTotal", result.size());
            return r;
        }

        if (!StringUtil.isEmpty(getSchoolVo.getMajor1()) || !StringUtil.isEmpty(getSchoolVo.getMajor2()) || !StringUtil.isEmpty(getSchoolVo.getMajor3())) {
            majorSchoolIdList = majorService.selectSchoolId(getSchoolVo);
            if (ObjectUtils.isEmpty(majorSchoolIdList)) {
                for (SchoolV2 schoolV2 : result) {
                    if (StringUtils.hasText(schoolV2.getSubject())) {
                        schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
                    }
                    getSchoolProbability(schoolV2, precedence);
                }
                R r = R.ok(result);
                r.put("searchTotal", 0);
                return r;
            }
        }
        List<String> codeList = new ArrayList<>();
        //办学性质||院校层级||博硕资格不为空||城市不为空
        if (!ObjectUtils.isEmpty(getSchoolVo.getNature()) || !ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications()) || !ObjectUtils.isEmpty(getSchoolVo.getCity())) {
            List<String> labelCode = new ArrayList<>();
            if (!ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications())) {
                List<SchoolLabel> labelList = schoolLabelService.getByParam(String.join("|", getSchoolVo.getHierarchys()), String.join("|", getSchoolVo.getQualifications()));
                labelList.forEach(i -> labelCode.add(i.getNewCode()));
            }

            String city = getSchoolVo.getCity().toString();
            List<SchoolLabel> schoolLabels;
            List<Object> citys = Arrays.asList("陕西省", "安徽省", "北京市", "重庆市", "福建省", "广东省", "广西", "甘肃省", "贵州省", "河北省",
                    "河南省", "海南省", "湖北省", "湖南省", "黑龙江省", "吉林省", "江苏省", "江西省", "辽宁省", "内蒙古", "宁夏", "青海省", "上海市",
                    "四川省", "山东省", "山西省", "天津市", "西藏", "新疆", "云南省", "浙江省");
            if (!StringUtil.isEmpty(getSchoolVo.getCity()) && city.equals("[其他]")) {
                schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                        .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getNewCode, labelCode)
                        .notIn(SchoolLabel::getCity, citys)
                        .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                        .select(SchoolLabel::getNewCode)
                );
            } else {
                schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                        .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getCode, labelCode)
                        .in(!StringUtil.isEmpty(getSchoolVo.getCity()), SchoolLabel::getCity, getSchoolVo.getCity())
                        .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                        .select(SchoolLabel::getNewCode)
                );
            }
            codeList = schoolLabels.stream().map(SchoolLabel::getNewCode).collect(Collectors.toList());
//            if (codeList.size() <= 0) {
//                PageInfo<SchoolV2> pageInfos = PageHelper.startPage(getSchoolVo.getPageNum(), getSchoolVo.getPageSize()).doSelectPageInfo(() -> {});
//                return R.ok(pageInfos);
//            }
        }


        String schoolType = getSchoolVo.getType().toString();
        List<Object> schoolTypes = Arrays.asList("综合", "工科", "财经", "农业", "林业", "医药", "师范", "体育", "政法", "民族", "语言", "艺术");

        LambdaQueryWrapper<SchoolV2> pageQuery = new LambdaQueryWrapper<SchoolV2>()
                .in(Objects.nonNull(majorSchoolIdList) && majorSchoolIdList.size() > 0, SchoolV2::getId, majorSchoolIdList)
                .in(!StringUtil.isEmpty(codeList), SchoolV2::getCode, codeList)
                .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), SchoolV2::getBatch, getSchoolVo.getBatch())
                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
                .in(!ObjectUtils.isEmpty(getSchoolVo.getRecruitType()), SchoolV2::getRecruitType, getSchoolVo.getRecruitType());

        if (!StringUtil.isEmpty(getSchoolVo.getType()) && schoolType.equals("[其他]")) {
            pageQuery.notIn(SchoolV2::getSchoolType, schoolTypes);
        } else {
            pageQuery.in(!StringUtil.isEmpty(getSchoolVo.getType()), SchoolV2::getSchoolType, getSchoolVo.getType());
        }
        List<String> pageSubject = new ArrayList<>();
        pageSubject.add("不限");
        for (String subjectParam : getSchoolVo.getSubject()) {
            pageSubject.add(subjectParam);
        }
        if(getSchoolVo.getSubject().size()>1){
            pageSubject.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
            pageSubject.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
        }
        pageQuery.in(SchoolV2::getSubject, pageSubject);


        List<SchoolV2> pageInfo = schoolService.list(pageQuery);

        for (SchoolV2 schoolV2 : result) {
            pageInfo.forEach(p -> {
                if (p.getId().equals(schoolV2.getId())) {
                    schoolV2.setColor(1);
                }
            });
            if (StringUtils.hasText(schoolV2.getSubject())) {
                schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
            }
            getSchoolProbability(schoolV2, precedence);
        }
        R r = R.ok(result);
        if (Objects.nonNull(pageInfo)) {
            r.put("searchTotal", pageInfo.size());
        } else {
            r.put("searchTotal", 0);
        }
        return r;
    }

    /**
     * 获取院校录取概率
     *
     * @param schoolV2
     * @param precedence
     */
    private void getSchoolProbability(SchoolV2 schoolV2, Integer precedence) {
        if (Objects.nonNull(precedence) && precedence > 0) {
            Map<String, String> probabilityMap = ProbabilityUtils.getProbability(schoolV2.getType(), schoolV2.getGroupProposedwc(), precedence);
            if (StringUtils.hasText(probabilityMap.get("probability"))) {
                schoolV2.setProbability(probabilityMap.get("probability"));
                try {
                    BigDecimal probabilityNum = new BigDecimal(probabilityMap.get("probability").replace("%", ""));
                    schoolV2.setProbabilityNum(probabilityNum);
                } catch (Exception e) {
                    logger.error("获取院校录取概率异常，异常信息：", e);
                    schoolV2.setProbabilityNum(new BigDecimal(0));
                }
                schoolV2.setProbabilityName(probabilityMap.get("probabilityName"));
            }
        }
    }

    /**
     * 获取专业录取概率
     *
     * @param majorV2
     * @param precedence
     */
    private void getMajorProbability(MajorV2 majorV2, Integer precedence) {
        if (Objects.nonNull(precedence) && precedence > 0) {
            Map<String, String> probabilityMap = ProbabilityUtils.getProbability(majorV2.getType(), majorV2.getMajorWeici(), precedence);
            if (StringUtils.hasText(probabilityMap.get("probability"))) {
                majorV2.setProbability(probabilityMap.get("probability"));
                try {
                    BigDecimal probabilityNum = new BigDecimal(probabilityMap.get("probability").replace("%", ""));
                    majorV2.setProbabilityNum(probabilityNum);
                } catch (Exception e) {
                    logger.error("获取专业录取概率异常，异常信息：", e);
                    majorV2.setProbabilityNum(new BigDecimal(0));
                }
                majorV2.setProbabilityName(probabilityMap.get("probabilityName"));
            }
        }
    }

    @PostMapping("/getSchoolMajor")
    public R getSchoolMajor(@RequestBody GetSchoolV2Vo getSchoolVo) {
        List<MajorV2> list = majorService.list(new LambdaQueryWrapper<MajorV2>()
                .eq(!StringUtil.isEmpty(getSchoolVo.getSchoolCode()), MajorV2::getCode, getSchoolVo.getSchoolCode())
                .eq(MajorV2::getType, getSchoolVo.getMajorType())
                .eq(MajorV2::getGroupCode, getSchoolVo.getGroupCode())
                .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), MajorV2::getBatch, getSchoolVo.getBatch())
        );
        Integer precedence = getSchoolVo.getPrecedence();
        if (Objects.isNull(precedence)) {
            Student student = studentService.getById(getSchoolVo.getStuId());
            if (Objects.nonNull(student) && !StringUtil.isEmpty(student.getPrecedence()) && StringUtil.isNumeric(student.getPrecedence())) {
                precedence = Integer.valueOf(student.getPrecedence());
            }
        }

        if (StringUtils.hasText(getSchoolVo.getMajor1()) || StringUtils.hasText(getSchoolVo.getMajor2()) || StringUtils.hasText(getSchoolVo.getMajor3())) {
            List<MajorV2> limit = majorService.list(new LambdaQueryWrapper<MajorV2>()
                    .and(wrapper ->
                            wrapper.like(!StringUtil.isEmpty(getSchoolVo.getMajor1()), MajorV2::getMajorName, getSchoolVo.getMajor1())
                                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor1()), MajorV2::getLevel, getSchoolVo.getMajor1())
                                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor2()), MajorV2::getMajorName, getSchoolVo.getMajor2())
                                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor2()), MajorV2::getLevel, getSchoolVo.getMajor2())
                                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor3()), MajorV2::getMajorName, getSchoolVo.getMajor3())
                                    .or().like(!StringUtil.isEmpty(getSchoolVo.getMajor3()), MajorV2::getLevel, getSchoolVo.getMajor3())
                    )
                    .eq(!StringUtil.isEmpty(getSchoolVo.getSchoolCode()), MajorV2::getCode, getSchoolVo.getSchoolCode())
                    .eq(MajorV2::getType, getSchoolVo.getMajorType())
                    .eq(MajorV2::getGroupCode, getSchoolVo.getGroupCode())
                    .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), MajorV2::getBatch, getSchoolVo.getBatch())
                    .select(MajorV2::getId)
            );
            for (MajorV2 majorV2 : list) {
                limit.forEach(m -> {
                    if (majorV2.getId().equals(m.getId())) {
                        majorV2.setColor("red");
                    }
                });
            }
        }
        for (MajorV2 majorV2 : list) {
            getMajorProbability(majorV2, precedence);
        }
        return R.ok(list);
    }

    @GetMapping("/getMajor")
    public R getMajor() {
        List<MajorV2> list = majorService.getMajorNameList();
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (MajorV2 i : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("value", i.getMajorName());
            result.add(map);
        }
        return R.ok(result);
    }

    //院校查询专业输入框提示
    @GetMapping("/getMajorName")
    public R getMajorName(@Param("majorName") String majorName) {
        List<MajorCategoryInfo> list = majorCategoryInfoService.list(new LambdaQueryWrapper<MajorCategoryInfo>()
                .eq(MajorCategoryInfo::getLevel, 3)
                .eq(MajorCategoryInfo::getCatType, 1)
                .like(!StringUtil.isEmpty(majorName), MajorCategoryInfo::getName, majorName)
        );
        Set<String> majorNameSet = new HashSet<>();

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (MajorCategoryInfo i : list) {
            if (majorNameSet.add(i.getName())) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("value", i.getName());
                result.add(map);
            }
        }
        return R.ok(result);
    }

    @PostMapping("/getSchoolName")
    public R getSchoolName(@Param("majorType") String majorType, @Param("batch") String batch, @Param("schoolName") String schoolName) {
        List<SchoolV2> list = schoolService.list(new LambdaQueryWrapper<SchoolV2>()
                .eq(!StringUtil.isEmpty(majorType), SchoolV2::getType, majorType)
                .eq(!StringUtil.isEmpty(batch), SchoolV2::getBatch, batch)
                .like(!StringUtil.isEmpty(schoolName), SchoolV2::getSchoolName, schoolName));

        Set<String> schoolNameSet = new HashSet<>();

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (SchoolV2 i : list) {
            if (schoolNameSet.add(i.getSchoolName())) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("value", i.getSchoolName());
                result.add(map);
            }
        }
        return R.ok(result);
    }


    @PostMapping("/getStuSchool")
    public R getStuSchool(@RequestBody GetSchoolV2Vo getSchoolVo) {
        List<Long> majorSchoolIdList = new ArrayList<>();

        List<SchoolV2> result = new ArrayList<>();

        if (ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
            return R.error("再选科目参数不能为空");
        }

        String redisKey = getSchoolVo.getBatch() + "_" + getSchoolVo.getMajorType() + "_" + getSchoolVo.getSortType();
        for (String subject : getSchoolVo.getSubject()) {
            redisKey += "_" + subject;
        }
        if (!ObjectUtils.isEmpty(getSchoolVo.getRecruitType())) {
            for (String recruitType : getSchoolVo.getRecruitType()) {
                redisKey += "_" + recruitType;
            }
        }

        if (StringUtils.hasText(stringRedisUtils.getStringVal(redisKey))) {
            result = JSONArray.parseArray(stringRedisUtils.getStringVal(redisKey), SchoolV2.class);
        } else {
            LambdaQueryWrapper<SchoolV2> query = new LambdaQueryWrapper<SchoolV2>()
                    .eq(SchoolV2::getBatch, getSchoolVo.getBatch())
                    .eq(SchoolV2::getBatch, getSchoolVo.getBatch())
                    .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
                    .in(!ObjectUtils.isEmpty(getSchoolVo.getRecruitType()), SchoolV2::getRecruitType, getSchoolVo.getRecruitType());

            List<String> subject = new ArrayList<>();
            subject.add("不限");
            for (String subjectParam : getSchoolVo.getSubject()) {
                subject.add(subjectParam);
            }
            if(getSchoolVo.getSubject().size()>1){
                subject.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
                subject.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
            }
            query.in(SchoolV2::getSubject, subject);

            if (getSchoolVo.getSortType() == 1) {
                query.orderByAsc(SchoolV2::getProposedwc);
            } else {
                query.orderByAsc(SchoolV2::getGroupProposedwc);
            }
            result = schoolService.list(query);
            stringRedisUtils.setStringVal(redisKey, JSONObject.toJSONString(result));
        }
        Integer precedence = getSchoolVo.getPrecedence();

        if (!StringUtil.isEmpty(getSchoolVo.getSchool())) {
            result = result.stream().filter(data -> data.getSchoolName().equals(getSchoolVo.getSchool())).collect(Collectors.toList());
            if(!StringUtil.isEmpty(getSchoolVo.getCheckGroupCode())){
                result = result.stream().filter(data -> data.getGroupCode().equals(getSchoolVo.getCheckGroupCode())).collect(Collectors.toList());
            }
            for (SchoolV2 schoolV2 : result) {
                if (StringUtils.hasText(schoolV2.getSubject())) {
                    schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
                }
                getSchoolProbability(schoolV2, precedence);
            }
            R r = R.ok(result);
            r.put("searchTotal", result.size());
            return r;
        }

        //判断是否有查询条件
        if (StringUtil.isEmpty(getSchoolVo.getMajor1()) && StringUtil.isEmpty(getSchoolVo.getMajor2()) && StringUtil.isEmpty(getSchoolVo.getMajor3())
                && StringUtil.isEmpty(getSchoolVo.getQualifications()) && StringUtil.isEmpty(getSchoolVo.getHierarchys()) && ObjectUtils.isEmpty(getSchoolVo.getNature())
                && ObjectUtils.isEmpty(getSchoolVo.getType()) && ObjectUtils.isEmpty(getSchoolVo.getCity())) {
            for (SchoolV2 schoolV2 : result) {
                if (StringUtils.hasText(schoolV2.getSubject())) {
                    schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
                }
                getSchoolProbability(schoolV2, precedence);
            }
            R r = R.ok(result);
            r.put("searchTotal", result.size());
            return r;
        }

        if (!StringUtil.isEmpty(getSchoolVo.getMajor1()) || !StringUtil.isEmpty(getSchoolVo.getMajor2()) || !StringUtil.isEmpty(getSchoolVo.getMajor3())) {
            majorSchoolIdList = majorService.selectSchoolId(getSchoolVo);
            if (ObjectUtils.isEmpty(majorSchoolIdList)) {
                R r = R.ok(new ArrayList<>());
                r.put("searchTotal", 0);
                return r;
            }
        }
        List<String> codeList = new ArrayList<>();
        //办学性质||院校层级||博硕资格不为空||城市不为空
        if (!ObjectUtils.isEmpty(getSchoolVo.getNature()) || !ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications()) || !ObjectUtils.isEmpty(getSchoolVo.getCity())) {
            List<String> labelCode = new ArrayList<>();
            if (!ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications())) {
                List<SchoolLabel> labelList = schoolLabelService.getByParam(String.join("|", getSchoolVo.getHierarchys()), String.join("|", getSchoolVo.getQualifications()));
                labelList.forEach(i -> labelCode.add(i.getNewCode()));
            }

            String city = getSchoolVo.getCity().toString();
            List<SchoolLabel> schoolLabels;
            List<Object> citys = Arrays.asList("陕西省", "安徽省", "北京市", "重庆市", "福建省", "广东省", "广西", "甘肃省", "贵州省", "河北省",
                    "河南省", "海南省", "湖北省", "湖南省", "黑龙江省", "吉林省", "江苏省", "江西省", "辽宁省", "内蒙古", "宁夏", "青海省", "上海市",
                    "四川省", "山东省", "山西省", "天津市", "西藏", "新疆", "云南省", "浙江省");
            if (!StringUtil.isEmpty(getSchoolVo.getCity()) && city.equals("[其他]")) {
                schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                        .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getNewCode, labelCode)
                        .notIn(SchoolLabel::getCity, citys)
                        .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                        .select(SchoolLabel::getNewCode)
                );
            } else {
                schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                        .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getCode, labelCode)
                        .in(!StringUtil.isEmpty(getSchoolVo.getCity()), SchoolLabel::getCity, getSchoolVo.getCity())
                        .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                        .select(SchoolLabel::getNewCode)
                );
            }
            codeList = schoolLabels.stream().map(SchoolLabel::getNewCode).collect(Collectors.toList());
//            if (codeList.size() <= 0) {
//                PageInfo<SchoolV2> pageInfos = PageHelper.startPage(getSchoolVo.getPageNum(), getSchoolVo.getPageSize()).doSelectPageInfo(() -> {});
//                return R.ok(pageInfos);
//            }
        }


        String schoolType = getSchoolVo.getType().toString();
        List<Object> schoolTypes = Arrays.asList("综合", "工科", "财经", "农业", "林业", "医药", "师范", "体育", "政法", "民族", "语言", "艺术");

        LambdaQueryWrapper<SchoolV2> pageQuery = new LambdaQueryWrapper<SchoolV2>()
                .in(Objects.nonNull(majorSchoolIdList) && majorSchoolIdList.size() > 0, SchoolV2::getId, majorSchoolIdList)
                .in(!StringUtil.isEmpty(codeList), SchoolV2::getCode, codeList)
                .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), SchoolV2::getBatch, getSchoolVo.getBatch())
                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
                .in(!ObjectUtils.isEmpty(getSchoolVo.getRecruitType()), SchoolV2::getRecruitType, getSchoolVo.getRecruitType());

        if (!StringUtil.isEmpty(getSchoolVo.getType()) && schoolType.equals("[其他]")) {
            pageQuery.notIn(SchoolV2::getSchoolType, schoolTypes);
        } else {
            pageQuery.in(!StringUtil.isEmpty(getSchoolVo.getType()), SchoolV2::getSchoolType, getSchoolVo.getType());
        }
        List<String> pageSubject = new ArrayList<>();
        pageSubject.add("不限");
        for (String subjectParam : getSchoolVo.getSubject()) {
            pageSubject.add(subjectParam);
        }
        if(getSchoolVo.getSubject().size()>1){
            pageSubject.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
            pageSubject.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
        }
        pageQuery.in(SchoolV2::getSubject, pageSubject);
        if (getSchoolVo.getSortType() == 1) {
            pageQuery.orderByAsc(SchoolV2::getProposedwc);
        } else {
            pageQuery.orderByAsc(SchoolV2::getGroupProposedwc);
        }


        List<SchoolV2> pageInfo = schoolService.list(pageQuery);

        for (SchoolV2 schoolV2 : pageInfo) {
            if (StringUtils.hasText(schoolV2.getSubject())) {
                schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
            }
            getSchoolProbability(schoolV2, precedence);
        }
        R r = R.ok(pageInfo);
        if (Objects.nonNull(pageInfo)) {
            r.put("searchTotal", pageInfo.size());
        } else {
            r.put("searchTotal", 0);
        }
        return r;
    }

    /**
     * 录取概率查询
     *
     * @param getSchoolVo
     * @return
     */
    @PostMapping("mobile/getSchoolAdmissionPro")
    public R getSchoolAdmissionPro(@RequestBody GetSchoolV2Vo getSchoolVo) {
        R r = R.ok();
        if (Objects.isNull(getSchoolVo.getPrecedence())) {
            r = R.error("位次不能为空");
            return r;
        }
        getSchoolVo.setPageNum((getSchoolVo.getPageNum() - 1) * getSchoolVo.getPageSize());
        List<SchoolInfoV2> list = schoolInfoV2Service.queryByParams(getSchoolVo);
        if (CollectionUtils.isEmpty(list)) {
            r = R.error("未查询到学校信息");
            return r;
        }

        r.put("schoolInfo", list.get(0));
        LambdaQueryWrapper<SchoolV2> query = new LambdaQueryWrapper<SchoolV2>()
                .eq(SchoolV2::getBatch, getSchoolVo.getBatch())
                .eq(SchoolV2::getSchoolName, getSchoolVo.getSchool())
                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType());

        List<SchoolV2> result = schoolService.list(query);

        for (SchoolV2 schoolV2 : result) {
            getSchoolProbability(schoolV2, getSchoolVo.getPrecedence());
            List<MajorV2> majorV2List = majorService.list(new LambdaQueryWrapper<MajorV2>()
                    .eq(MajorV2::getCode, schoolV2.getCode())
                    .eq(MajorV2::getType, getSchoolVo.getMajorType())
                    .eq(MajorV2::getGroupCode, schoolV2.getGroupCode())
                    .eq(MajorV2::getBatch, getSchoolVo.getBatch())
            );
            for (MajorV2 majorV2 : majorV2List) {
                if(StringUtils.hasText(majorV2.getMinShiftLine2017()) && !"暂无数据".equals(majorV2.getMinShiftLine2017())){
                    getMajorProbability(majorV2, getSchoolVo.getPrecedence());
                }
            }
            schoolV2.setMajorV2List(majorV2List);
        }

        r.put("schoolDataList", result);
        return r;
    }

    /**
     * 手机端学生分页获取院校
     *
     * @param getSchoolVo
     * @return
     */
    @PostMapping("mobile/getStuSchoolPage")
    public R getStuSchoolPage(HttpServletRequest request, @RequestBody GetSchoolV2Vo getSchoolVo) {
        List<Long> majorSchoolIdList = new ArrayList<>();

        List<SchoolV2> result = new ArrayList<>();

        if (ObjectUtils.isEmpty(getSchoolVo.getSubject())) {
            return R.error("再选科目参数不能为空");
        }
        getSchoolVo.setSortType(2);

        Integer stuWeici = getStuShemeWeici(request, getSchoolVo);

        LambdaQueryWrapper<SchoolV2> query = new LambdaQueryWrapper<SchoolV2>()
                .eq(SchoolV2::getBatch, getSchoolVo.getBatch())
                .eq(!StringUtil.isEmpty(getSchoolVo.getSchoolName()), SchoolV2::getSchoolName, getSchoolVo.getSchoolName())
                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
                .in(!ObjectUtils.isEmpty(getSchoolVo.getRecruitType()), SchoolV2::getRecruitType, getSchoolVo.getRecruitType())
                .gt(stuWeici != null && stuWeici>0, SchoolV2::getGroupProposedwc, stuWeici);
        List<String> subject = new ArrayList<>();
        subject.add("不限");
        for (String subjectParam : getSchoolVo.getSubject()) {
            subject.add(subjectParam);
        }
        if (getSchoolVo.getSubject().size() > 1) {
            subject.add(getSchoolVo.getSubject().get(0) + "," + getSchoolVo.getSubject().get(1));
            subject.add(getSchoolVo.getSubject().get(1) + "," + getSchoolVo.getSubject().get(0));
        }
        query.in(SchoolV2::getSubject, subject);

        if (getSchoolVo.getSortType() == 1) {
            query.orderByAsc(SchoolV2::getProposedwc);
        } else {
            query.orderByAsc(SchoolV2::getGroupProposedwc);
        }
        PageInfo<SchoolV2> resultInfo = PageHelper.startPage(getSchoolVo.getPageNum(), getSchoolVo.getPageSize())
                .doSelectPageInfo(() -> {
                    List<SchoolV2> list = schoolService.list(query);
                });
        result = resultInfo.getList();


        Integer precedence = getSchoolVo.getPrecedence();

        if(!StringUtil.isEmpty(getSchoolVo.getSchoolName())){
            for (SchoolV2 schoolV2 : result) {
                //获取学校的附加信息
                schoolV2.setSchoolInfo(schoolInfoV2Service.selectBySchoolName(schoolV2.getSchoolName()));
                if (StringUtils.hasText(schoolV2.getSubject())) {
                    schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
                }
                getSchoolProbability(schoolV2, precedence);
            }
            R r = R.ok(result);
            r.put("searchTotal", resultInfo.getTotal());
            return r;
        }
        //判断是否有查询条件
        if (StringUtil.isEmpty(getSchoolVo.getMajor1()) && StringUtil.isEmpty(getSchoolVo.getMajor2()) && StringUtil.isEmpty(getSchoolVo.getMajor3())
                && StringUtil.isEmpty(getSchoolVo.getQualifications()) && StringUtil.isEmpty(getSchoolVo.getHierarchys()) && ObjectUtils.isEmpty(getSchoolVo.getNature())
                && ObjectUtils.isEmpty(getSchoolVo.getType()) && ObjectUtils.isEmpty(getSchoolVo.getCity()) && ObjectUtils.isEmpty(getSchoolVo.getSchoolName())) {
            for (SchoolV2 schoolV2 : result) {
                //获取学校的附加信息
                schoolV2.setSchoolInfo(schoolInfoV2Service.selectBySchoolName(schoolV2.getSchoolName()));
                if (StringUtils.hasText(schoolV2.getSubject())) {
                    schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
                }
                getSchoolProbability(schoolV2, precedence);
            }
            R r = R.ok(result);
            r.put("searchTotal", resultInfo.getTotal());
            return r;
        }

        if (!StringUtil.isEmpty(getSchoolVo.getMajor1()) || !StringUtil.isEmpty(getSchoolVo.getMajor2()) || !StringUtil.isEmpty(getSchoolVo.getMajor3())) {
            majorSchoolIdList = majorService.selectSchoolId(getSchoolVo);
            if (ObjectUtils.isEmpty(majorSchoolIdList)) {
                R r = R.ok(new ArrayList<>());
                r.put("searchTotal", 0);
                return r;
            }
        }
        List<String> codeList = new ArrayList<>();
        //办学性质||院校层级||博硕资格不为空||城市不为空
        if (!ObjectUtils.isEmpty(getSchoolVo.getNature()) || !ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications()) || !ObjectUtils.isEmpty(getSchoolVo.getCity())) {
            List<String> labelCode = new ArrayList<>();
            if (!ObjectUtils.isEmpty(getSchoolVo.getHierarchys()) || !ObjectUtils.isEmpty(getSchoolVo.getQualifications())) {
                List<SchoolLabel> labelList = schoolLabelService.getByParam(String.join("|", getSchoolVo.getHierarchys()), String.join("|", getSchoolVo.getQualifications()));
                labelList.forEach(i -> labelCode.add(i.getNewCode()));
            }

            String city = getSchoolVo.getCity().toString();
            List<SchoolLabel> schoolLabels;
            List<Object> citys = Arrays.asList("陕西省", "安徽省", "北京市", "重庆市", "福建省", "广东省", "广西", "甘肃省", "贵州省", "河北省",
                    "河南省", "海南省", "湖北省", "湖南省", "黑龙江省", "吉林省", "江苏省", "江西省", "辽宁省", "内蒙古", "宁夏", "青海省", "上海市",
                    "四川省", "山东省", "山西省", "天津市", "西藏", "新疆", "云南省", "浙江省");
            if (!StringUtil.isEmpty(getSchoolVo.getCity()) && city.equals("[其他]")) {
                schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                        .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getNewCode, labelCode)
                        .notIn(SchoolLabel::getCity, citys)
                        .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                        .select(SchoolLabel::getNewCode)
                );
            } else {
                schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                        .in(!StringUtil.isEmpty(labelCode), SchoolLabel::getCode, labelCode)
                        .in(!StringUtil.isEmpty(getSchoolVo.getCity()), SchoolLabel::getCity, getSchoolVo.getCity())
                        .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                        .select(SchoolLabel::getNewCode)
                );
            }
            codeList = schoolLabels.stream().map(SchoolLabel::getNewCode).collect(Collectors.toList());
//            if (codeList.size() <= 0) {
//                PageInfo<SchoolV2> pageInfos = PageHelper.startPage(getSchoolVo.getPageNum(), getSchoolVo.getPageSize()).doSelectPageInfo(() -> {});
//                return R.ok(pageInfos);
//            }
        }


        String schoolType = getSchoolVo.getType().toString();
        List<Object> schoolTypes = Arrays.asList("综合", "工科", "财经", "农业", "林业", "医药", "师范", "体育", "政法", "民族", "语言", "艺术");

        LambdaQueryWrapper<SchoolV2> pageQuery = new LambdaQueryWrapper<SchoolV2>()
                .in(Objects.nonNull(majorSchoolIdList) && majorSchoolIdList.size() > 0, SchoolV2::getId, majorSchoolIdList)
                .in(!StringUtil.isEmpty(codeList), SchoolV2::getCode, codeList)
                .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), SchoolV2::getBatch, getSchoolVo.getBatch())
                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
                .in(!ObjectUtils.isEmpty(getSchoolVo.getRecruitType()), SchoolV2::getRecruitType, getSchoolVo.getRecruitType())
                .gt(stuWeici != null && stuWeici>0, SchoolV2::getGroupProposedwc, stuWeici);

        if (!StringUtil.isEmpty(getSchoolVo.getType()) && schoolType.equals("[其他]")) {
            pageQuery.notIn(SchoolV2::getSchoolType, schoolTypes);
        } else {
            pageQuery.in(!StringUtil.isEmpty(getSchoolVo.getType()), SchoolV2::getSchoolType, getSchoolVo.getType());
        }
        List<String> pageSubject = new ArrayList<>();
        pageSubject.add("不限");
        for (String subjectParam : getSchoolVo.getSubject()) {
            pageSubject.add(subjectParam);
        }
        if(getSchoolVo.getSubject().size()>1){
            pageSubject.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
            pageSubject.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
        }
        pageQuery.in(SchoolV2::getSubject, pageSubject);
        if (getSchoolVo.getSortType() == 1) {
            pageQuery.orderByAsc(SchoolV2::getProposedwc);
        } else {
            pageQuery.orderByAsc(SchoolV2::getGroupProposedwc);
        }

        PageInfo<SchoolV2> pageInfo = PageHelper.startPage(getSchoolVo.getPageNum(), getSchoolVo.getPageSize())
                .doSelectPageInfo(() -> {
                    List<SchoolV2> list = schoolService.list(pageQuery);
                });

        for (SchoolV2 schoolV2 : pageInfo.getList()) {
            //获取学校的附加信息
            schoolV2.setSchoolInfo(schoolInfoV2Service.selectBySchoolName(schoolV2.getSchoolName()));
            if (StringUtils.hasText(schoolV2.getSubject())) {
                schoolV2.setSubject(schoolV2.getSubject().replaceAll(",", "+"));
            }
            getSchoolProbability(schoolV2, precedence);
        }
        R r = R.ok(pageInfo.getList());
        if (Objects.nonNull(pageInfo)) {
            r.put("searchTotal", pageInfo.getTotal());
        } else {
            r.put("searchTotal", 0);
        }
        return r;
    }

    /**
     * 获取学生方案位次
     * @param request
     * @return
     */
    private Integer getStuShemeWeici(HttpServletRequest request, GetSchoolV2Vo getSchoolVo) {
        if(!getSchoolVo.getBatch().equals("本科普通批平行") && !getSchoolVo.getBatch().equals("高职专科普通批")){
            return null;
        }
        Long userId = getUserId(request);
        Student student = studentService.selectByUserId(userId);
        if(student != null && StringUtils.hasText(student.getScore())){
            try{
                Integer addScore = Integer.valueOf(student.getScore()) + 30;
                if (addScore > 720) {
                    addScore = 720;
                }
                List<ScoreItem> list = scoreItemService.list(new LambdaQueryWrapper<ScoreItem>()
                        .eq(ScoreItem::getType, student.getMajor())
                        .eq(ScoreItem::getScore, addScore)
                        .orderByDesc(ScoreItem::getYear)
                );
                if(list != null && list.size()>0){
                    return list.get(0).getLowWeici();
                }
            }catch (Exception e){
                logger.info("获取学生方案位次异常，异常信息=", e);
            }
        }
        return null;
    }


    @PostMapping("/getSchool")
    public R getSchool(@RequestBody GetSchoolV2Vo getSchoolVo) {
        List<String> majorCodeList = new ArrayList<String>();
        List<MajorV2> majors1 = majorService.list(new LambdaQueryWrapper<MajorV2>()
                .like(!StringUtil.isEmpty(getSchoolVo.getMajor1()), MajorV2::getMajorName, getSchoolVo.getMajor1())
                .eq(MajorV2::getType, getSchoolVo.getMajorType())
                .select(MajorV2::getCode, MajorV2::getId)
                .eq(MajorV2::getBatch, getSchoolVo.getBatch())
        );
        List<MajorV2> majors2 = majorService.list(new LambdaQueryWrapper<MajorV2>()
                .like(!StringUtil.isEmpty(getSchoolVo.getMajor2()), MajorV2::getMajorName, getSchoolVo.getMajor2())
                .eq(MajorV2::getType, getSchoolVo.getMajorType())
                .select(MajorV2::getCode, MajorV2::getId)
                .eq(MajorV2::getBatch, getSchoolVo.getBatch())
        );
        List<MajorV2> majors3 = majorService.list(new LambdaQueryWrapper<MajorV2>()
                .like(!StringUtil.isEmpty(getSchoolVo.getMajor3()), MajorV2::getMajorName, getSchoolVo.getMajor3())
                .eq(MajorV2::getType, getSchoolVo.getMajorType())
                .select(MajorV2::getCode, MajorV2::getId)
                .eq(MajorV2::getBatch, getSchoolVo.getBatch())
        );
        if (!StringUtil.isEmpty(getSchoolVo.getMajor1()) || !StringUtil.isEmpty(getSchoolVo.getMajor2()) || !StringUtil.isEmpty(getSchoolVo.getMajor1())) {
            if (StringUtil.isEmpty(getSchoolVo.getMajor1())) {
                majors1 = new ArrayList<MajorV2>();
            }
            if (StringUtil.isEmpty(getSchoolVo.getMajor2())) {
                majors2 = new ArrayList<MajorV2>();
            }
            if (StringUtil.isEmpty(getSchoolVo.getMajor3())) {
                majors3 = new ArrayList<MajorV2>();
            }
        }
        if (majors1.size() > 0 || majors2.size() > 0 || majors3.size() > 0) {
            if (majors1.size() > 0) {
                majors1.forEach(i -> majorCodeList.add(i.getCode()));
            }
            if (majors2.size() > 0) {
                majors2.forEach(i -> majorCodeList.add(i.getCode()));
            }
            if (majors3.size() > 0) {
                majors3.forEach(i -> majorCodeList.add(i.getCode()));
            }
        } else {
            List<SchoolV2> nullSchool = new ArrayList<>();
            return R.ok(nullSchool);
        }
        List<String> Hierarchyscode = new ArrayList<String>();
        List<String> qualificationscode = new ArrayList<String>();
        if (!StringUtil.isEmpty(getSchoolVo.getHierarchys())) {
            List<SchoolLabel> Hierarchyss = schoolLabelService.getcollegeLevel(String.join("|", getSchoolVo.getHierarchys()));
            if (Hierarchyss.size() > 0) {
                Hierarchyss.forEach(i -> Hierarchyscode.add(i.getCode()));
            }
        }
        if (!StringUtil.isEmpty(getSchoolVo.getQualifications())) {
            List<SchoolLabel> qualificationss = schoolLabelService.getqualifications(String.join("|", getSchoolVo.getQualifications()));
            if (qualificationss.size() > 0) {
                qualificationss.forEach(i -> qualificationscode.add(i.getCode()));
            }
        }
        String city = getSchoolVo.getCity().toString();
        List<SchoolLabel> schoolLabels;
        List<Object> citys = Arrays.asList("陕西省", "安徽省", "北京市", "重庆市", "福建省", "广东省", "广西", "甘肃省", "贵州省", "河北省",
                "河南省", "海南省", "湖北省", "湖南省", "黑龙江省", "吉林省", "江苏省", "江西省", "辽宁省", "内蒙古", "宁夏", "青海省", "上海市",
                "四川省", "山东省", "山西省", "天津市", "西藏", "新疆", "云南省", "浙江省");
//        System.out.println("集合"+citys);
        if (!StringUtil.isEmpty(getSchoolVo.getCity()) && city.equals("[其他]")) {
            schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                    .notIn(SchoolLabel::getCity, citys)
                    .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                    .in(!StringUtil.isEmpty(Hierarchyscode), SchoolLabel::getCode, Hierarchyscode)
                    .in(!StringUtil.isEmpty(qualificationscode), SchoolLabel::getCode, qualificationscode)
                    .select(SchoolLabel::getCode)
            );
        } else {
            schoolLabels = schoolLabelService.list(new LambdaQueryWrapper<SchoolLabel>()
                    .in(!StringUtil.isEmpty(getSchoolVo.getCity()), SchoolLabel::getCity, getSchoolVo.getCity())
                    .in(!StringUtil.isEmpty(getSchoolVo.getNature()), SchoolLabel::getNature, getSchoolVo.getNature())
                    .in(!StringUtil.isEmpty(Hierarchyscode), SchoolLabel::getCode, Hierarchyscode)
                    .in(!StringUtil.isEmpty(qualificationscode), SchoolLabel::getCode, qualificationscode)
                    .select(SchoolLabel::getCode)
            );
        }

        List<String> codeList = schoolLabels.stream().map(SchoolLabel::getCode).collect(Collectors.toList());
        if (!StringUtil.isEmpty(getSchoolVo.getMaxranking()) && !StringUtil.isEmpty(getSchoolVo.getMinranking())) {
            getSchoolVo.setMaxfraction(null);
            getSchoolVo.setMinfraction(null);
        }
        if (codeList.size() <= 0) {
            PageInfo<SchoolV2> pageInfos = PageHelper.startPage(getSchoolVo.getPageNum(), getSchoolVo.getPageSize()).doSelectPageInfo(() -> {
            });
            return R.ok(pageInfos);
        }
        String schoolType = getSchoolVo.getType().toString();
        List<SchoolV2> pageInfo;
        List<Object> schoolTypes = Arrays.asList("综合", "工科", "财经", "农业", "林业", "医药", "师范", "体育", "政法", "民族", "语言"
                , "艺术");
        if (!StringUtil.isEmpty(getSchoolVo.getType()) && schoolType.equals("[其他]")) {
            pageInfo = schoolService.list(new LambdaQueryWrapper<SchoolV2>()
                            .in(!StringUtil.isEmpty(majorCodeList), SchoolV2::getCode, majorCodeList)
                            .in(!StringUtil.isEmpty(codeList), SchoolV2::getCode, codeList)
                            .notIn(SchoolV2::getSchoolType, schoolTypes)
                            .like(!StringUtil.isEmpty(getSchoolVo.getSchool()), SchoolV2::getSchoolName, getSchoolVo.getSchool())
                            .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), SchoolV2::getBatch, getSchoolVo.getBatch())
                            .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
//                    .ge(!StringUtil.isEmpty(getSchoolVo.getMinfraction()), School::getProposedScore, getSchoolVo.getMinfraction())
//                    .le(!StringUtil.isEmpty(getSchoolVo.getMaxfraction()), School::getProposedScore, getSchoolVo.getMaxfraction())
//                    .ge(!StringUtil.isEmpty(getSchoolVo.getMaxranking()), School::getProposedwc, getSchoolVo.getMaxranking())
//                    .le(!StringUtil.isEmpty(getSchoolVo.getMinranking()), School::getProposedwc, getSchoolVo.getMinranking())
            );
        } else {
            pageInfo = schoolService.list(new LambdaQueryWrapper<SchoolV2>()
                            .in(!StringUtil.isEmpty(majorCodeList), SchoolV2::getCode, majorCodeList)
                            .in(!StringUtil.isEmpty(codeList), SchoolV2::getCode, codeList)
                            .in(!StringUtil.isEmpty(getSchoolVo.getType()), SchoolV2::getSchoolType, getSchoolVo.getType())
                            .like(!StringUtil.isEmpty(getSchoolVo.getSchool()), SchoolV2::getSchoolName, getSchoolVo.getSchool())
                            .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), SchoolV2::getBatch, getSchoolVo.getBatch())
                            .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
//                    .ge(!StringUtil.isEmpty(getSchoolVo.getMinfraction()), School::getProposedScore, getSchoolVo.getMinfraction())
//                    .le(!StringUtil.isEmpty(getSchoolVo.getMaxfraction()), School::getProposedScore, getSchoolVo.getMaxfraction())
//                    .ge(!StringUtil.isEmpty(getSchoolVo.getMaxranking()), School::getProposedwc, getSchoolVo.getMaxranking())
//                    .le(!StringUtil.isEmpty(getSchoolVo.getMinranking()), School::getProposedwc, getSchoolVo.getMinranking())
            );
        }
        List<SchoolV2> relust = schoolService.list(new LambdaQueryWrapper<SchoolV2>()
                        .eq(SchoolV2::getBatch, getSchoolVo.getBatch())
                        .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
//                .ge(!StringUtil.isEmpty(getSchoolVo.getMinfraction()), School::getProposedScore, getSchoolVo.getMinfraction())
//                .le(!StringUtil.isEmpty(getSchoolVo.getMaxfraction()), School::getProposedScore, getSchoolVo.getMaxfraction())
//                .ge(!StringUtil.isEmpty(getSchoolVo.getMaxranking()), School::getProposedwc, getSchoolVo.getMaxranking())
//                .le(!StringUtil.isEmpty(getSchoolVo.getMinranking()), School::getProposedwc, getSchoolVo.getMinranking())
                        .orderByAsc(SchoolV2::getProposedwc)
        );
        relust.forEach(i -> {
            pageInfo.forEach(p -> {
                if (p.getId().equals(i.getId())) {
                    i.setColor(1);
                }
            });
        });
        return R.ok(relust);
    }


    @PostMapping("/getSchools")
    public R getSchools(@RequestBody GetSchoolV2Vo getSchoolVo) {
        if (!StringUtil.isEmpty(getSchoolVo.getMaxranking()) && !StringUtil.isEmpty(getSchoolVo.getMinranking())) {
            getSchoolVo.setMaxfraction(null);
            getSchoolVo.setMinfraction(null);
        }
        List<SchoolV2> pageInfo;
        pageInfo = schoolService.list(new LambdaQueryWrapper<SchoolV2>()
                        .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), SchoolV2::getBatch, getSchoolVo.getBatch())
                        .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType())
//                    .ge(!StringUtil.isEmpty(getSchoolVo.getMinfraction()), School::getProposedScore, getSchoolVo.getMinfraction())
//                    .le(!StringUtil.isEmpty(getSchoolVo.getMaxfraction()), School::getProposedScore, getSchoolVo.getMaxfraction())
//                    .ge(!StringUtil.isEmpty(getSchoolVo.getMaxranking()), School::getProposedwc, getSchoolVo.getMaxranking())
//                    .le(!StringUtil.isEmpty(getSchoolVo.getMinranking()), School::getProposedwc, getSchoolVo.getMinranking())
                        .orderByAsc(SchoolV2::getProposedwc)

        );
        //统计分页后的学校编号
//        if (!getSchoolVo.getBatch().equals("提前批A段") && !getSchoolVo.getBatch().equals("提前批B段") && !getSchoolVo.getBatch().equals("提前批C段") && !getSchoolVo.getBatch().equals("提前批专科") && !getSchoolVo.getBatch().equals("一批特殊") && !getSchoolVo.getBatch().equals("二批特殊")) {
//            if (!StringUtil.isEmpty(getSchoolVo.getRanking())) {
//                for (School s : pageInfo) {
//                    if (s.getProposedwc() <= (Integer.parseInt(getSchoolVo.getRanking()) * 1.1)) {
//                        s.setSprint("冲");
//                    } else {
//                        if (getSchoolVo.getBatch().equals("本科二批")) {
//                            if ((s.getProposedwc() > (Integer.parseInt(getSchoolVo.getRanking()) * 1.1)) && (s.getProposedwc() <= (Integer.parseInt(getSchoolVo.getRanking()) * 1.4))) {
//                                s.setSprint("稳");
//                            } else if (s.getProposedwc() > (Integer.parseInt(getSchoolVo.getRanking()) * 1.4)) {
//                                s.setSprint("保");
//                            }
//                        } else {
//                            if ((s.getProposedwc() > (Integer.parseInt(getSchoolVo.getRanking()) * 1.1)) && (s.getProposedwc() <= (Integer.parseInt(getSchoolVo.getRanking()) * 1.6))) {
//                                s.setSprint("稳");
//                            } else if (s.getProposedwc() > (Integer.parseInt(getSchoolVo.getRanking()) * 1.6)) {
//                                s.setSprint("保");
//                            }
//                        }
//                    }
//                }
//            } else if (StringUtil.isEmpty(getSchoolVo.getRanking())&&!StringUtil.isEmpty(getSchoolVo.getFraction())) {
//                String ranking = "";
//                if (getSchoolVo.getMajorType() == 1) {
//                    List<SysDict> km = sysDictService.list(new LambdaQueryWrapper<SysDict>().
//                            eq(SysDict::getDictType, "rankingli"));
//                    for (SysDict sysDict : km) {
//                        if (sysDict.getDictName().equals(getSchoolVo.getFraction())) {
//                            ranking = String.valueOf(sysDict.getDictValue());
//                        }
//                    }
//                } else if (getSchoolVo.getMajorType() == 2) {
//                    List<SysDict> km = sysDictService.list(new LambdaQueryWrapper<SysDict>().
//                            eq(SysDict::getDictType, "rankingwen"));
//                    for (SysDict sysDict : km) {
//                        if (sysDict.getDictName().equals(getSchoolVo.getFraction())) {
//                            ranking = String.valueOf(sysDict.getDictValue());
//                        }
//                    }
//                }
//                for (School s : pageInfo) {
//                    if (s.getProposedwc() <= (Integer.parseInt(ranking) * 1.1)) {
//                        s.setSprint("冲");
//                    } else {
//                        if (getSchoolVo.getBatch().equals("本科二批")) {
//                            if ((s.getProposedwc() > (Integer.parseInt(ranking) * 1.1)) && (s.getProposedwc() <= (Integer.parseInt(ranking) * 1.4))) {
//                                s.setSprint("稳");
//                            } else if (s.getProposedwc() > (Integer.parseInt(ranking) * 1.4)) {
//                                s.setSprint("保");
//                            }
//                        } else {
//                            if ((s.getProposedwc() > (Integer.parseInt(ranking) * 1.1)) && (s.getProposedwc() <= (Integer.parseInt(ranking) * 1.6))) {
//                                s.setSprint("稳");
//                            } else if (s.getProposedwc() > (Integer.parseInt(ranking) * 1.6)) {
//                                s.setSprint("保");
//                            }
//                        }
//                    }
//                }
//            }
//        }
        return R.ok(pageInfo);
    }


    @PostMapping("/getGroupCode")
    public R getGroupCode(@RequestBody GetSchoolV2Vo getSchoolVo) {
        LambdaQueryWrapper<SchoolV2> query = new LambdaQueryWrapper<SchoolV2>()
                .eq(!StringUtil.isEmpty(getSchoolVo.getSchool()), SchoolV2::getSchoolName, getSchoolVo.getSchool())
                .eq(!StringUtil.isEmpty(getSchoolVo.getBatch()), SchoolV2::getBatch, getSchoolVo.getBatch())
                .eq(getSchoolVo.getMajorType() != null, SchoolV2::getType, getSchoolVo.getMajorType());

        if ("本科普通批平行".equals(getSchoolVo.getBatch())) {
            query.in(!ObjectUtils.isEmpty(getSchoolVo.getRecruitType()), SchoolV2::getRecruitType, getSchoolVo.getRecruitType());
        }
        List<String> subject = new ArrayList<>();
        subject.add("不限");
        for (String subjectParam : getSchoolVo.getSubject()) {
            subject.add(subjectParam);
        }
        if(getSchoolVo.getSubject().size()>1){
            subject.add(getSchoolVo.getSubject().get(0)+","+getSchoolVo.getSubject().get(1));
            subject.add(getSchoolVo.getSubject().get(1)+","+getSchoolVo.getSubject().get(0));
        }
        query.in(SchoolV2::getSubject, subject);

        List<SchoolV2> list = schoolService.list(query);

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (SchoolV2 i : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("value", i.getGroupCode());
            map.put("label", i.getGroupCode());
            result.add(map);
        }
        return R.ok(result);
    }

    /**
     * 手机端获取学校录取数据
     *
     * @param code  院校code
     * @param batch 批次名称
     * @param type  1:物理 2:历史
     * @return
     */
    @GetMapping("/mobile/getAdmissionData")
    public R getAdmissionData(String code, String batch, String type) {
        List<String> queryBatchs = new ArrayList<>();
        if ("本科批次".equals(batch)) {
            queryBatchs.add("本科普通批平行");
            queryBatchs.add("本科普通批单设");
        } else if ("提前批次".equals(batch)) {
            queryBatchs.add("本科提前批平行");
            queryBatchs.add("本科提前批单设");
        } else {
            queryBatchs.add(batch);
        }
        //获取学校录取数据
        List<SchoolV2> schools = schoolService.list(new LambdaQueryWrapper<SchoolV2>()
                .eq(SchoolV2::getCode, code)
                .in(SchoolV2::getBatch, queryBatchs)
                .eq(SchoolV2::getType, type)
        );

        for (SchoolV2 schoolV2 : schools) {
            List<MajorV2> list = majorService.list(new LambdaQueryWrapper<MajorV2>()
                    .eq(MajorV2::getCode, schoolV2.getCode())
                    .eq(MajorV2::getType, schoolV2.getType())
                    .eq(MajorV2::getGroupCode, schoolV2.getGroupCode())
                    .eq(MajorV2::getBatch, schoolV2.getBatch())
            );
            schoolV2.setMajorV2List(list);

            schoolV2.setSchoolInfo(schoolInfoV2Service.selectBySchoolName(schoolV2.getSchoolName()));
        }
        return R.ok(schools);
    }
}
